package com.task.manager.service.home.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.task.manager.domain.Task;
import com.task.manager.mapper.TaskMapper;
import com.task.manager.service.home.TaskService;
import com.task.manager.util.TaskUtil;

@Service
public class TaskServiceImpl implements TaskService {

	private static final Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);

	@Autowired
	private TaskMapper taskMapper;

	@Override
	public List<Task> queryTaskByParam(String taskCode, String taskName) {
		Task record = new Task();
		if (!StringUtils.isEmpty(taskCode) || !StringUtils.isEmpty(taskName)) {
			record.setTaskCode(TaskUtil.convertEmptyStringToNull(taskCode));
			record.setTaskName(TaskUtil.convertEmptyStringToNull(taskName));
			List<Task> tasks = taskMapper.queryTaskByParam(record);
			return tasks;
		} else {
			List<Task> tasks = taskMapper.queryAll();
			return tasks;
		}
	}

	@Override
	public Boolean insertTask(Task task) {
		try {
			int i = taskMapper.insert(task);
			return i == 1;
		} catch (Exception e) {
			logger.error("TaskServiceImpl_insertTask_error:" + e.getMessage(), e);
		}

		return false;
	}

	@Override
	public Boolean updateTask(Task task) {
		try {
			int i = taskMapper.updateByPrimaryKey(task);
			return i == 1;
		} catch (Exception e) {
			logger.error("TaskServiceImpl_updateTask_error:" + e.getMessage(), e);
		}

		return false;
	}

	@Override
	public Boolean deleteTask(Integer id) {
		try {
			int i = taskMapper.deleteByPrimaryKey(id);
			return i == 1;
		} catch (Exception e) {
			logger.error("TaskServiceImpl_updateTask_error:" + e.getMessage(), e);
		}

		return false;
	}

	@Override
	public List<Task> queryAll() {
		return taskMapper.queryAll();
	}

}
